package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Business;
import cn.wolfcode.qo.ConsumptionReportQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionReportService;
import cn.wolfcode.vo.ConsumptionReportVO;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/consumptionReport")
public class ConsumptionReportController {
    @Autowired
    public IConsumptionReportService consumptionReportService;
    @Autowired
    public IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo){
        PageInfo<ConsumptionReportVO> pageInfo = consumptionReportService.list(qo);
        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses",businesses);
        model.addAttribute("pageInfo",pageInfo);
        return "consumptionReport/list";
    }
        @RequestMapping("/listBar")
    public String listBar(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo){
            qo.setPageSize(0);
            ArrayList<String> groupTypes = new ArrayList<>();
            ArrayList<BigDecimal> totalAmounts = new ArrayList<>();
            ArrayList<BigDecimal> totalDiscountAmounts = new ArrayList<>();
            ArrayList<BigDecimal> totalPayAmounts = new ArrayList<>();
            ArrayList<Integer> counts = new ArrayList<>();
            List<ConsumptionReportVO> data = consumptionReportService.list(qo).getList();
            for (ConsumptionReportVO vo : data) {
                groupTypes.add(vo.getGroupType());
                totalAmounts.add(vo.getTotalAmount());
                totalDiscountAmounts.add(vo.getTotalDiscountAmount());
                totalPayAmounts.add(vo.getTotalPayAmount());
                counts.add(vo.getTotalCount());
            }
        model.addAttribute("groupTypes", JSON.toJSONString(groupTypes));
        model.addAttribute("totalAmounts",JSON.toJSONString(totalAmounts));
        model.addAttribute("totalDiscountAmounts",JSON.toJSONString(totalDiscountAmounts));
        model.addAttribute("totalPayAmounts",JSON.toJSONString(totalPayAmounts));
        model.addAttribute("counts",JSON.toJSONString(counts));
        return "consumptionReport/listBar";
    }









}
